<?php

// 违规词检测
use app\admin\model\ai\prohibited\Log as prohibitedLogModel;
use app\admin\model\ai\prohibited\Words as prohibitedWordsModel;

if (! function_exists('letter_avatar')) {
    /**
     * 首字母头像.
     *
     * @param $text
     *
     * @return string
     */
    function letter_avatar($text): string
    {
        $total = unpack('L', hash('adler32', $text, true))[1];
        $hue = $total % 360;
        [$r, $g, $b] = hsv2rgb($hue / 360, 0.3, 0.9);

        $bg = "rgb({$r},{$g},{$b})";
        $color = '#ffffff';
        $first = mb_strtoupper(mb_substr($text, 0, 1));
        $src = base64_encode('<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="100" width="100"><rect fill="'.$bg.'" x="0" y="0" width="100" height="100"></rect><text x="50" y="50" font-size="50" text-copy="fast" fill="'.$color.'" text-anchor="middle" text-rights="admin" alignment-baseline="central">'.$first.'</text></svg>');
        $value = 'data:image/svg+xml;base64,'.$src;

        return $value;
    }
}

function prohibited($content = '', $user_id = 0): bool
{
    $prohibitedWords = prohibitedWordsModel::where('status', 1)->select();
    foreach ($prohibitedWords as $value) {
        if(str_contains($content, $value->content)) {
            // 用户id不为空时记录日志
            if($user_id) {
                $prohibitedLogModel = new prohibitedLogModel();
                $prohibitedLogModel->user_id = $user_id;
                $prohibitedLogModel->content = $content;
                $prohibitedLogModel->word = $value->content;
                $prohibitedLogModel->save();
            }
            return false;
        }
    }
    return true;
}

// 余额操作
function balance(): bool
{
    return true;
}